<template>
	<el-main>
		<el-card class="box-card" shadow="never">
			<div class="search-box">
				<el-form :inline="true" :model="searchForm" class="demo-form-inline">
					<el-form-item label="商品参数">
						<el-input v-model="searchForm.name" placeholder="" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit" icon="el-icon-search">查询</el-button>
					</el-form-item>
				</el-form>
			</div>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-bottom: 10px"></div>
			<el-button type="primary" icon="el-icon-plus" @click="addAttr" style="margin-top: 10px">添加参数</el-button>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-top: 10px"></div>
			<el-table
				:data="tableData"
				style="width: 100%;">
				<el-table-column
					type=""
					prop="id"
					label="ID">
				</el-table-column>
				<el-table-column
					prop="name"
					label="商品参数值">
				</el-table-column>
				<el-table-column
					label="参数名">
					<template #default="scope">
						<p v-for="item in scope.row.attr.item" :key="item.id">{{ item }}</p>
					</template>
				</el-table-column>
				<el-table-column
					label="商品参数">
					<template #default="scope">
						<p v-for="item in scope.row.attr.value" :key="item.id">{{ item }}</p>
					</template>
				</el-table-column>
				<el-table-column
					prop="operation"
					label="操作">
					<template #default="scope">
						<el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
						<el-button @click="handleDel(scope.row)" type="text" size="small">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<Pages :form="searchForm" :page-obj="page" @pageChange="handlePageChange" @pageJump="getList"></Pages>

			<el-dialog :title="title" v-model="dialogVisible" width="900px" :close-on-click-modal="false">
				<el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
					<el-form-item label="模板名称" prop="name">
						<el-input v-model="form.name"></el-input>
					</el-form-item>
					<el-form-item label="参数">
						<el-button type="primary" icon="el-icon-plus" size="small" @click="addNewItem">新的参数
						</el-button>
						<el-table
							:data="attrTable"
							border
							style="width: 100%;margin-top: 10px">
							<el-table-column
								label="参数名">
								<template #default="scope">
									<el-input v-model="scope.row.item"></el-input>
								</template>
							</el-table-column>
							<el-table-column
								label="参数值">
								<template #default="scope">
									<el-input v-model="scope.row.value"></el-input>
								</template>
							</el-table-column>
							<el-table-column
								width="100px"
								label="操作">
								<template #default="scope">
									<i class="el-icon-delete" @click="delAttr(scope.$index)"
									   style="cursor: pointer"></i>
									<el-icon>
										<component :is="deleteIcon" style="cursor: pointer"
												   @click="delAttr(scope.$index)" class="el-icon-delete"/>
									</el-icon>
								</template>
							</el-table-column>
						</el-table>
					</el-form-item>
					<el-form-item>
						<el-button @click="dialogVisible = false">取 消</el-button>
						<el-button type="primary" @click="optSubmit('ruleForm')" :loading="loading">确 定</el-button>
					</el-form-item>
				</el-form>
			</el-dialog>
		</el-card>
	</el-main>
</template>

<script>
import Pages from "@/components/pages/index.vue";

export default {
	components: {Pages},
	data() {
		return {
			searchForm: {
				name: '',
				limit: 15,
				page: 1
			},
			dialogVisible: false,
			title: '新增商品参数',
			page: {
				total: 0
			},
			tableData: [],
			dialogCateVisible: false,
			form: {
				id: 0,
				name: '',
				item: [],
				value: []
			},
			attrTable: [
				{item: '', value: ''}
			],
			rules: {
				name: [
					{required: true, message: '请输入模板名称', trigger: 'blur'}
				]
			},
			loading: false,
			deleteIcon: 'el-icon-delete'
		}
	},
	mounted() {
		this.getList()
	},
	methods: {
		// 获取列表
		async getList() {
			let res = await this.$API.goodsAttr.list.get(this.searchForm)
			this.tableData = res.data.data
			this.page.total = res.data.total
		},
		// 初始化表单
		initForm() {
			this.form = {
				id: 0,
				name: '',
				item: [],
				value: []
			}
			this.attrTable = [
				{item: '', value: ''}
			]
		},
		// 添加参数
		addAttr() {
			this.title = '新增商品参数'
			this.initForm()
			this.dialogVisible = true
		},
		// 添加信息规格
		addNewItem() {
			this.attrTable.push({item: '', value: ''})
		},
		// 删除规格
		delAttr(index) {
			this.attrTable.splice(index, 1)
		},
		// 搜索
		onSubmit() {
			this.searchForm.page = 1
			this.getList()
		},
		// 编辑参数
		handleEdit(item) {
			this.title = '编辑商品参数'
			this.dialogVisible = true

			this.form.id = item.id
			this.form.name = item.name

			this.attrTable = []
			item.attr.item.forEach((vo, index) => {
				this.attrTable.push({
					item: vo, value: item.attr.value[index]
				})
			})
		},
		// 删除参数
		handleDel(item) {
			this.$confirm('此操作将永久删除该商品参数, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				let res = await this.$API.goodsAttr.del.get({id: item.id})
				if (res.code == 0) {
					this.$message.success(res.msg)
					this.getList()
				} else {
					this.$message.error(res.msg)
				}
			}).catch(() => {
			});
		},
		// 分页
		handlePageChange(page) {
			this.searchForm.page = page
			this.getList()
		},
		// 新增或编辑
		optSubmit(formName) {
			this.$refs[formName].validate(async (valid) => {
				if (valid) {
					this.form.item = [];
					this.form.value = [];
					this.attrTable.forEach(item => {
						if (item.item != '') {
							this.form.item.push(item.item)
							this.form.value.push(item.value)
						}
					})

					this.loading = true
					let res;
					if (this.form.id) {
						res = await this.$API.goodsAttr.edit.post(this.form)
					} else {
						res = await this.$API.goodsAttr.add.post(this.form)
					}
					this.loading = false
					if (res.code == 0) {
						this.dialogVisible = false
						this.$message.success(res.msg)
						this.form = {
							id: 0,
							name: '',
							item: [],
							value: []
						}
						this.getList()
					} else {
						this.$message.error(res.msg)
					}
				}
			});
		}
	}

}
</script>

<style scoped>
.el-table__header tr,
.el-table__header th {
	padding: 0;
	height: 20px;
	line-height: 20px;
}

p {
	padding: 0;
	margin: 0;
}
.el-button--small {
	padding: 0 !important;
}
</style>
